﻿using System;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Common.Utils;
using Telerik.WebControls;

public partial class sysadmin_DL_Project_AddTrainType : PowerCreator.LMS.Web.Base.WebBasePage
{
    Course opCourse = new Course();
    TOTrainType obj = new TOTrainType();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            int SID = 0; 
            if (Request.QueryString["SID"] != null)
            {
                SID = int.Parse(Request.QueryString["SID"].ToString());
                ViewState["SID"] = SID.ToString();
                BinderForm(SID);
            }
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup != 1)
                {
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }


    }
    protected override void InitPage()
    {
        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderForm(int id)
    {
        VOTOTrainType vo = new VOTOTrainType();
        vo = obj.Load(id);
        txtNewTypeName.Text = vo.TrainTypeName;
        txt_ShortName.Text = vo.ShortName;
        txt_pic.Text = vo.PicUrl;
        ckb_Isshow.Checked = vo.IsShow;
    }
    private bool CheckPageData()
    {
        string search = string.Format("TrainTypeName = '{0}'", txtNewTypeName.Text);
        if (ViewState["SID"] != null)
        {
            search += "and ID<>" + ViewState["SID"].ToString();
        }
        if (obj.Count(search) != 0)
        {
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOTrainType vo = new VOTOTrainType();
        if (ViewState["SID"] == null)
        {
            vo.TrainTypeName = txtNewTypeName.Text.Trim();
            vo.ShortName = txt_ShortName.Text;
            vo.PicUrl = txt_pic.Text;
            vo.IsShow = ckb_Isshow.Checked;
            if (obj.Add(vo))
            {
                string url = "ManageTrainType.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = obj.Load(int.Parse(ViewState["SID"].ToString()));
            vo.TrainTypeName = txtNewTypeName.Text.Trim();
            vo.ShortName = txt_ShortName.Text;
            vo.PicUrl = txt_pic.Text;
            vo.IsShow = ckb_Isshow.Checked;
            if (obj.Update(vo))
            {
                string url = "ManageTrainType.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageTrainType.aspx";
        Script.RedirectURL(url);
    }




    protected void btnBack_Click(object sender, EventArgs e)
    {
        // btn_Back.Enabled = false;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Url = "";
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/TrainTypePic/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);
            txt_pic.Text = Url;
            btn_Back.Enabled = true;
        }
    }
    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "gif|jpg";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            lbl_FileUpload_msg_FileType.Text = GetLanageValueByKey("AddCourseInfo_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + allowExtensions;
            return false;
        }
        return true;
    }
}
